//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package mail_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

type MailBodyDbItem struct {
	MailId string `bson:"_id"`
	Text   string `bson:"text"`
	Html   string `bson:"html"`
}

type _MailBodyDao struct {
	mc *mongo.Collection
}

func newMailBodyDao(db *mongo.Database) (*_MailBodyDao, error) {
	mc := db.Collection(db_ns.DB_NS_MAIL + "mail_body")
	return &_MailBodyDao{mc: mc}, nil
}

func (db *_MailBodyDao) Insert(ctx context.Context, dbItem *MailBodyDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_MailBodyDao) Get(ctx context.Context, mailId string) (*MailBodyDbItem, error) {
	retItem := &MailBodyDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": mailId}).Decode(retItem)
	return retItem, err
}

func (db *_MailBodyDao) Remove(ctx context.Context, mailId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": mailId})
	return err
}

func (db *_MailBodyDao) RemoveById(ctx context.Context, mailIdList []string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"_id": bson.M{"$in": mailIdList}})
	return err
}
